//
//  ConPublishViewController.m
//  Shengzhuli
//
//  Created by subook on 2017/8/16.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConPublishViewController.h"
#import "YYTextView.h"
#import "TakePhotoTool.h"
#import "MBProgressHUD+MJ.h"
#import "PersonNetWork.h"
#import "SZLAccountHelper.h"
#import "GFXNetworkHandle.h"
#import "LPDQuoteImagesView.h"
#import "PickUpNetwork.h"

@interface ConPublishViewController ()<LPDQuoteImagesViewDelegate>
{
    UITextField *tf;
    YYTextView *tv;
    NSArray *imageArray;//存放image
    LPDQuoteImagesView *quoteImagesView;
    NSString *urlString;
    NSMutableArray *stringArray;
    int num;
}
@end

@implementation ConPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    num = 0;
    imageArray= [NSArray array];
    stringArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"编辑内容";
    [self configNavi];
    [self configUI];
    
}

- (void)configNavi {
    
    UIButton *publishButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    publishButton.frame = CGRectMake(0, 0, 30, 30);
    [publishButton setTitle:@"发表" forState:(UIControlStateNormal)];
    [publishButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    publishButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [publishButton addTarget:self action:@selector(publishButtonClicked) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem *naviRightItem = [[UIBarButtonItem alloc] initWithCustomView:publishButton];
    self.navigationItem.rightBarButtonItem = naviRightItem;
    
}
//点击发表按钮
- (void)publishButtonClicked {
    
    imageArray = [NSArray arrayWithArray:quoteImagesView.selectedPhotos];
    
    if (tf.text.length <1) {
        [MBProgressHUD showError:@"请输入标题"];
    }else if (tv.text.length<1) {
        [MBProgressHUD showError:@"请输入正文"];
    }else {
        if (imageArray.count>0) {
            //如果有图片
            for (int i =0; i<imageArray.count; i++) {
                
                [PersonNetWork upLoadImageWithParame:@{@"optionType":@"6"} image:imageArray[i] response:^(BOOL success, NSString *msg, NSString *path) {
                    
                    if (success) {
                        [stringArray addObject:path];
                        
                        num++;
                        if (num == imageArray.count) {
                            [self upload];
                        }
                    }
                    else {
                        [MBProgressHUD showError:msg];
                    }
                    NSLog(@"urlstring = %@",urlString);
                    
                }];
            }
            //如果没图片
        }else {
            [self upload];
        }
        
    }
}
- (void)upload {
    
    urlString = [stringArray componentsJoinedByString:@"#"];
    
    NSLog(@"++++++++++%@",urlString);
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"token"] = [SZLAccountHelper shareInstance].token;
    param[@"ruleTitle"] = tf.text;
    param[@"ruleContent"] = tv.text;
    param[@"rulePictureUrl"] = urlString;
    [PickUpNetwork getUpPickWithParame:param response:^(BOOL success, NSString *msg, NSMutableArray *arr) {
        
        if (success) {
            [MBProgressHUD showSuccess:msg];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (void)configUI {
    tf = [[UITextField alloc] initWithFrame:(CGRectMake(15, 10, ScreenW-30, 30))];
    tf.placeholder = @"输入标题";
    [self.view addSubview:tf];
    
    tv = [[YYTextView alloc] initWithFrame:(CGRectMake(15, tf.frame.origin.y+30+10, ScreenW-30, 70))];
    tv.font = [UIFont systemFontOfSize:14];
    tv.placeholderText = @"输入正文";
    [self.view addSubview:tv];

    quoteImagesView =[[LPDQuoteImagesView alloc] initWithFrame:CGRectMake(15, tv.frame.origin.y+70+10, ScreenW-100, ScreenW/3) withCountPerRowInView:3 cellMargin:12];
    //初始化view的frame, view里每行cell个数， cell间距（上方的图片1 即为quoteImagesView）
    quoteImagesView.maxSelectedCount = 3;
    //最大可选照片数
    quoteImagesView.collectionView.scrollEnabled = NO;
    //view可否滑动
    quoteImagesView.navcDelegate = self;    //self 至少是一个控制器。
    //委托（委托controller弹出picker，且不用实现委托方法）
    [self.view addSubview:quoteImagesView];
    //把view加到某一个视图上，就什么都不用管了！！！！
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
